package com.swx.identityservice.service.impl;


import com.swx.identityservice.mapper.ScbpUserMapper;
import com.swx.identityservice.models.ScbpPermission;
import com.swx.identityservice.models.ScbpRolePermission;
import com.swx.identityservice.models.ScbpUser;
import com.swx.identityservice.models.ScbpUserPermission;
import com.swx.identityservice.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author LC
 * @version V1.0.0
 * @date 2018-6-14 10:07
 */
@Service
@Slf4j
public class CustomUserServiceImpl implements UserDetailsService { //自定义UserDetailsService 接口

    @Autowired
    private RedisService redisService;
    private static final Long MINUTES_EXPIRETIME = 60 * 60 * 1000L * 24;//一天过期

    @Autowired
    private ScbpUserMapper scbpUserMapper;

    @Autowired
    private ScbpRolePermissionService scbpRolePermissionService;
    @Autowired
    private ScbpUserPermissionService scbpUserPermissionService;
    @Autowired
    private ScbpPermissionService scbpPermissionService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException{ //重写loadUserByUsername 方法获得 userdetails 类型用户
        ScbpUser scbpUser = scbpUserMapper.selectUserByName(username);
        if (scbpUser == null) throw new UsernameNotFoundException("用户名不存在");
        //该人员权限由两部分组成，1.该人员拥有的所有角色对应的权限
        String roleids = scbpUser.getRoleids();
        String[] rolesArray = roleids.split(",");
        //存放该用户角色的所有权限id,角色权限用户权限取并集
        Set<Integer> authoritiesSet = new HashSet<>();
        for (int i = 0; i < rolesArray.length; i++) {
            List<ScbpRolePermission> rolePermissions = scbpRolePermissionService.getpermissionbyroleid(Integer.valueOf(rolesArray[i]));
            for (int j = 0; j < rolePermissions.size(); j++) {
                authoritiesSet.add(rolePermissions.get(j).getPermissionid());
            }
        }
        //该人员权限由两部分组成，2.该人员自己的权限
        List<ScbpUserPermission> userPermissions = scbpUserPermissionService.getpermissionbyuserid(scbpUser.getId());
        List<ScbpPermission> permissions = new ArrayList<>();
        userPermissions.stream().forEach(n -> {
            authoritiesSet.add(n.getPermissionid());
        });
        for (Integer set : authoritiesSet) {
            ScbpPermission scbpPermission = scbpPermissionService.selectByPrimaryKey(set);
            permissions.add(scbpPermission);
        }
        LoginUser loginUser = new LoginUser();
        BeanUtils.copyProperties(scbpUser, loginUser);
        loginUser.setPermissions(permissions);
        return loginUser;
    }
}

